package com.unproy.model.domain;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;


@Entity
@Table(name = "TBMTYP")
public class MediaType implements Serializable, Comparable<MediaType> {
	private static final long	serialVersionUID	= 4283118779162526647L;
	private Short				mediaTypeId;
	private String				description;
	private Set<Media>			media				= new HashSet<Media>();
	private Set<Article>		articles			= new HashSet<Article>();
	
	@Id
	@SequenceGenerator(name = "mediaTypeId", sequenceName = "SQMTYP")
	@GeneratedValue(generator = "mediaTypeId", strategy = GenerationType.SEQUENCE)
	@Column(name = "MTYPID", unique = true, nullable = false)
	public Short getMediaTypeId() {
		return this.mediaTypeId;
	}
	
	public void setMediaTypeId(Short mediaTypeId) {
		this.mediaTypeId = mediaTypeId;
	}
	
	@Column(name = "DSCRPT", nullable = false)
	public String getDescription() {
		return this.description;
	}
	
	public void setDescription(String description) {
		this.description = description;
	}
	
	@OneToMany(fetch = FetchType.LAZY, mappedBy = "mediaType")
	public Set<Media> getMedia() {
		return this.media;
	}
	
	public void setMedia(Set<Media> media) {
		this.media = media;
	}
	
	@ManyToMany(fetch = FetchType.LAZY, mappedBy = "library")
	public Set<Article> getArticles() {
		return this.articles;
	}
	
	public void setArticles(Set<Article> articles) {
		this.articles = articles;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((this.description == null) ? 0 : this.description.hashCode());
		result = prime * result + ((this.mediaTypeId == null) ? 0 : this.mediaTypeId.hashCode());
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		MediaType other = (MediaType) obj;
		if (this.description == null) {
			if (other.description != null)
				return false;
		} else if (!this.description.equals(other.description))
			return false;
		if (this.mediaTypeId == null) {
			if (other.mediaTypeId != null)
				return false;
		} else if (!this.mediaTypeId.equals(other.mediaTypeId))
			return false;
		return true;
	}
	
	@Override
	public int compareTo(MediaType mediaType) {
		return getDescription().compareTo(mediaType.getDescription());
	}
	
	@Override
	public String toString() {
		return getDescription();
	}
}
